package com.douwong.student.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.model.PrepareResource;

import java.util.ArrayList;


public class PrepareDetailResourceAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<PrepareResource> list;
	
	public PrepareDetailResourceAdapter(Context context, ArrayList<PrepareResource>list){
		this.context=context;
		this.list=list;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder=null;
		if(convertView==null){
			holder=new Holder();
			convertView=LayoutInflater.from(context).inflate(R.layout.prepare_resource_item, null);
			holder.imgView=(ImageView) convertView.findViewById(R.id.resource_img);
			holder.name_text=(TextView) convertView.findViewById(R.id.name_text);
			holder.load_img=(ImageView) convertView.findViewById(R.id.load_img);
			holder.loaded_img=(ImageView) convertView.findViewById(R.id.loaded_img);
			convertView.setTag(holder);
		}else {
			holder=(Holder) convertView.getTag();
		}
		if(list.size()>0){
			if(list.get(position).isLoading()){
				holder.load_img.setVisibility(View.VISIBLE);
				AnimationDrawable animationDrawable = null;
				holder.load_img.setBackgroundResource(R.drawable.load_anim);
				animationDrawable = (AnimationDrawable) holder.load_img.getBackground();
				animationDrawable.start();
			}else {
				holder.load_img.setVisibility(View.INVISIBLE);
			}
			if(list.get(position).isLoaded()){
				holder.loaded_img.setVisibility(View.VISIBLE);
			}else {
				holder.loaded_img.setVisibility(View.GONE);
			}
			holder.imgView.setImageResource(getIconByName(list.get(position).getAttaSuffix()));
			holder.name_text.setText(list.get(position).getFileName());
		}
		return convertView;
	}
	class Holder {
		ImageView imgView,load_img,loaded_img;
		TextView name_text;
	}
	private int getIconByName(String name){
		String str=name.toLowerCase();
		int id=R.mipmap.ft_txt;
		if(str.equals("jpg")||str.equals("png")){
			id=R.mipmap.pic_1;
		}else if(str.equals("mp4")||str.equals("avi")||str.equals("flv")){
			id=R.mipmap.veo_1;
		}else if(str.equals("pdf")){
			id=R.mipmap.ft_pdf;
		}else if(str.equals("ppt")){
			id=R.mipmap.ft_ppt;
		}else if(str.equals("doc")||str.equals("docx")){
			id=R.mipmap.ft_doc1;
		}else if(str.equals("swf")){
			id=R.mipmap.ft_swf;
		}else if(str.equals("xlsx")){
			id=R.mipmap.ft_xlsx;
		}else if(str.equals("zip")){
			id=R.mipmap.ft_zip;
		}else if(str.equals("xls")){
			id=R.mipmap.ft_xls;
		}
		return id;
		
	}

}
